//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    if (wx.cloud) {
      console.log("初始化")
      wx.cloud.init({
        env: 'linda-job-7g96rwuv26f6f90f',
        traceUser: true
      })
    }
    this.getOpenid()
  },
  globalData: {
    userInfo: '',
  },
  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        var openid = res.result.openId;
        that.setCache('openid', openid)
        const db = wx.cloud.database()
        db.collection('users').where({
            _openid: openid,
          })
          .get({
            success: function (res) {
              console.log(res)
              if(res.data.length>0){
                that.globalData.userInfo =  res.data[0];
                that.setCache('token', true)
              }else{
                that.setCache('token', false)
              }
            }
          })
      }
    })
  },
  setCache(key, val) { // 存储缓存
    wx.setStorageSync(key, val)
  },
  getCache(key) { // 获取缓存
    return wx.getStorageSync(key)
  },
})